<?php

use Drupal\shusy_gift_card\Entity\ShusyGiftCard;

function shusy_gift_card_shusy_gift_card_entity_form($form, &$form_state, ShusyGiftCard $e, $op) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_gift_card') . '/shusy_gift_card.forms.inc';
  $form_state[ShusyGiftCard::class] = $e;

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('保存'),
  ];
  return $form;
}

function shusy_gift_card_shusy_gift_card_entity_form_submit($form, &$form_state) {
  $e = $form_state[ShusyGiftCard::class];
  if ($e instanceof ShusyGiftCard) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $e->save();
  }
}

function shusy_gift_card_shusy_gift_card_delete_form($form, &$form_state, ShusyGiftCard $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_gift_card') . '/shusy_gift_card.forms.inc';
  $form_state[ShusyGiftCard::class] = $e;
  return confirm_form($form,
    t('Are you sure you want to delete %title?', ['%title' => $e->label()]),
    'admin/content/shusy_gift_card/' . $e->id,
    t('This action cannot be undone.'),
    t('Delete'),
    t('Cancel')
  );
}

function shusy_gift_card_shusy_gift_card_delete_form_submit($form, &$form_state) {
  $e = $form_state[ShusyGiftCard::class];
  if ($e instanceof ShusyGiftCard) {
    $e->delete();
  }
  $form_state['redirect'] = 'admin/content/shusy_gift_card';
}

function shusy_gift_card_shusy_store_provide_form($form, &$form_state, ShusyGiftCard $e) {
  $form_state['build_info']['files']['form'] = drupal_get_path('module', 'shusy_gift_card') . '/shusy_gift_card.forms.inc';
  $form_state[ShusyGiftCard::class] = $e;

  $form['name'] = [
    '#type' => 'textfield',
    '#title' => t('名称'),
    '#required' => TRUE,
    '#maxlength' => 32,
    '#size' => 32,
  ];
  $form['amount'] = [
    '#type' => 'textfield',
    '#title' => t('面值'),
    '#required' => TRUE,
    '#maxlength' => 8,
    '#size' => 8,
    '#element_validate' => ['element_validate_number'],
  ];
  $form['number'] = [
    '#type' => 'textfield',
    '#title' => t('数量'),
    '#required' => TRUE,
    '#default_value' => 1,
    '#maxlength' => 8,
    '#size' => 8,
    '#element_validate' => ['element_validate_integer'],
  ];

  field_attach_form($e->entityType(), $e, $form, $form_state);

  $form['actions'] = ['#type' => 'actions'];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => t('发放'),
  ];
  return $form;
}

function shusy_gift_card_shusy_store_provide_form_submit($form, &$form_state) {
  $e = $form_state[ShusyGiftCard::class];
  if ($e instanceof ShusyGiftCard) {
    entity_form_submit_build_entity($e->entityType(), $e, $form, $form_state);
    $number = $form_state['values']['number'];
    for ($i = 0; $i < $number; $i++) {
      $clone = new ShusyGiftCard();
      entity_form_submit_build_entity($clone->entityType(), $clone, $form, $form_state);
      $clone->amount = ceil($e->amount * 100);
      $clone->status = 0;
      $clone->uid = $e->uid;
      $clone->save();
    }
  }
}
